package com.ljkj.cordial.chat.adapter;

import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.CheckBox;
import android.widget.FrameLayout;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.ljkj.cordial.chat.R;
import com.ljkj.cordial.chat.dao.UserTableBean;
import com.ljkj.cordial.chat.dao.notify.FriendTable;
import com.ljkj.cordial.chat.fresco.ImageLoader;
import com.ljkj.cordial.chat.widget.PuzzleImageView;
import com.nic.view.adapter.CommonCheckAdapter;
import com.nic.view.model.CheckType;
import com.nic.view.model.CheckViewHolder;

import butterknife.BindView;
import butterknife.ButterKnife;


/**
 * Created by Administrator on 2019/4/15.
 */

public class PersonnelAdapter extends CommonCheckAdapter<FriendTable, CheckViewHolder> {
    //显示头部
    private static final int VIEW_TYPE_ITEM_TITLE = 0;
    //显示内容
    private static final int VIEW_TYPE_ITEM_CONTENT = 1;


    public PersonnelAdapter(CheckType checkType) {
        super(checkType);
    }

    @Override
    protected int getTag(int itemViewType) {
        if (itemViewType == VIEW_TYPE_ITEM_TITLE) {
            return R.id.view_title;
        }
        return R.id.view_content;
    }

//    @Override
//    public void initCheckBox(ViewGroup parent, CheckBox checkBox) {
//        super.initCheckBox(parent, checkBox);
//        if (checkBox != null) {
//            checkBox.setTextColor(ContextCompat.getColorStateList(parent.getContext(), com.nic.view.R.color.tv_white_gray));
//            checkBox.setBackground(new ColorDrawable(Color.TRANSPARENT));
//            checkBox.setButtonDrawable(R.drawable.cb_circle_blue);
//        }
//
//    }

    @Override
    public CheckViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        if (viewType == VIEW_TYPE_ITEM_TITLE) {
            View rootView = LayoutInflater.from(parent.getContext()).inflate(R.layout.item_contact_title, null);
            return new ViewTitleHolder(rootView);
        }
        View rootView = LayoutInflater.from(parent.getContext()).inflate(R.layout.item_contact_person, null);
        ViewPersonnelHolder holder = new ViewPersonnelHolder(rootView);
        return holder;
    }

    @Override
    public void onBindViewHolder(ViewGroup parent, final CheckViewHolder holder, final int position) {
        final FriendTable item = getList().get(position);
        if (holder instanceof ViewTitleHolder) {
            ViewTitleHolder viewTitleHolder = (ViewTitleHolder) holder;
            viewTitleHolder.tvTitle.setText("" + item.getFirstLetter());
        } else {
            final ViewPersonnelHolder viewPersonnelHolder = (ViewPersonnelHolder) holder;

            holder.getRootView().setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {
//                    if (CheckType.NONE.equals(getCheckType())) {
//                        FriendInfoActivity.jumpTo(view.getContext(), IMSContacts.UserType.REGISTERED_USER.equals(item.getUserType()) ? Canstants.KEY_REGISTER : Canstants.KEY_VISITOR, item.getFriendNickname(), item.getFriendUserId(), item.getUserType());
//                    }
                    if (onItemClickListener != null) {
                        onItemClickListener.onItemClick(view, item, position);
                    }
                }
            });

            viewPersonnelHolder.itemTitle.setText(TextUtils.isEmpty(item.getFriendAlias()) ? item.getFriendNickname() : item.getFriendAlias());
            viewPersonnelHolder.itemTitle2.setText("");
            if (UserTableBean.TYPE_VISITOR.equals(item.getUserType())) {
                viewPersonnelHolder.itemContent.setVisibility(View.VISIBLE);
                viewPersonnelHolder.itemContent.setText("此好友是游客身份，请注意安全");
                viewPersonnelHolder.tvTan.setVisibility(View.VISIBLE);
                viewPersonnelHolder.ivIsVisitor.setVisibility(View.VISIBLE);
            } else {
                viewPersonnelHolder.itemContent.setVisibility(View.GONE);
                viewPersonnelHolder.tvTan.setVisibility(View.GONE);
                viewPersonnelHolder.ivIsVisitor.setVisibility(View.GONE);
            }
//            if (TextUtils.isEmpty(item.getFriendAvatar())) {
//                viewPersonnelHolder.itemImg.setImageResource(R.mipmap.pic_user_avatar_default_36dp);
//            } else {
//                viewPersonnelHolder.itemImg.setFriendUrl(item.getFriendAvatar(), item.getFriendUserId());
//            }
//            ImageLoader.getInstance().displayFriendAvatar(item.getFriendUserId(), item.getFriendAvatar(), viewPersonnelHolder.itemImg);
//            viewPersonnelHolder.itemImg.setFriendPath(item.getFriendAvatarPath());
//            if (!TextUtils.isEmpty(item.getFriendAvatarPath())) {
//                viewPersonnelHolder.itemImg.setFriendPath(item.getFriendAvatarPath(),item.getFriendAvatar());
//            } else {
//
//            }
            if (!TextUtils.isEmpty(item.getFriendAvatarPath())) {
                viewPersonnelHolder.itemImg.setFriendPath(item.getFriendAvatarPath(),item.getFriendAvatar());
            }else{
                viewPersonnelHolder.itemImg.setFriendUrl(item.getFriendAvatar(),item.getFriendUserId());
            }

            if (getCheckType() != CheckType.NONE) {
                holder.getRootView().setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        setCheckStateListener(position, holder.create(), true);
                    }
                });
            }

        }
    }

    public void upDateItemView(ListView listView, int position) {
        int visiblePosition = listView.getFirstVisiblePosition();
        View convertView = listView.getChildAt(position - visiblePosition);
        getView(position - visiblePosition, convertView, listView);
    }

    @Override
    public int getItemViewType(int position) {
        FriendTable pb = getList().get(position);
        if (pb.isShowTitle()) {
            return VIEW_TYPE_ITEM_TITLE;
        } else {
            return VIEW_TYPE_ITEM_CONTENT;
        }
    }


    @Override
    public String getItemBeanId(int position) {
        return getList().get(position).getFriendUserId();
    }

    @Override
    public String getItemBeanName(int position) {
        return getList().get(position).getFriendNickname();
    }


    static class ViewTitleHolder extends CheckViewHolder {
        @BindView(R.id.tv_title)
        TextView tvTitle;

        public ViewTitleHolder(View view) {
            super(view);
            ButterKnife.bind(this, view);
        }

        @Override
        public CheckBox create() {
            return null;
        }
    }

    static class ViewPersonnelHolder extends CheckViewHolder {
        @BindView(R.id.item_cb)
        CheckBox itemCb;
        @BindView(R.id.item_img)
        PuzzleImageView itemImg;
        @BindView(R.id.item_title)
        TextView itemTitle;
        @BindView(R.id.item_title2)
        TextView itemTitle2;
        @BindView(R.id.item_content)
        TextView itemContent;
        @BindView(R.id.item_right_arrow)
        ImageView itemRightArrow;
        @BindView(R.id.ll_group_text)
        RelativeLayout llGroupText;
        @BindView(R.id.frame_avatar)
        FrameLayout frameAvatar;
        @BindView(R.id.tv_tan)
        TextView tvTan;
        @BindView(R.id.iv_is_visitor)
        ImageView ivIsVisitor;

        ViewPersonnelHolder(View view) {
            super(view);
            ButterKnife.bind(this, view);
        }

        @Override
        public CheckBox create() {
            return itemCb;
        }
    }

    private OnItemClickListener onItemClickListener;

    public void setOnItemClickListener(OnItemClickListener onItemClickListener) {
        this.onItemClickListener = onItemClickListener;
    }

    public interface OnItemClickListener {
        void onItemClick(View view, Object obj, int position);
    }
}
